package com.nus;



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;


import com.nus.parsers.ParserAPIs;
import com.nus.representations.IvleGradeBook;
import com.nus.representations.IvleModule;
import com.nus.representations.IvleTimeTable;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class IvleGradeActivity extends Activity{
	private ListView lv;

	private String lv_arr[];
	 protected TextView title;
	private ArrayList<IvleGradeBook> grade;
	 public void onCreate(Bundle savedInstanceState) {
		 
	        super.onCreate(savedInstanceState);
			requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.ivlegradebooklayout);
		 getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
		 title = (TextView) findViewById(R.id.titleheader);
		 title.setText("Grade Book");
		 ImageView b = (ImageView)findViewById(R.id.home);
		  b.setImageDrawable(getResources().getDrawable(R.drawable.ivle));
	      String url= "https://ivle.nus.edu.sg/api/Lapi.svc/Gradebook_ViewItems?APIKey=W7RtgW7GZKUpaEkJ1NwUD&AuthToken="+NUS_Android.IVLE_Token+"&CourseID="+IvleModuleActivity.COURSE_ID+"&output=json";
//	        String url = "https://ivle.nus.edu.sg/api/Lapi.svc/Modules?APIKey=W7RtgW7GZKUpaEkJ1NwUD&AuthToken="+NUS_Android.TOKEN+"&Duration=0&IncludeAllInfo=false&output=json";
	      try {
				String response = getResponseText(url);

				Log.i("Response", response);

				grade = ParserAPIs.parseIvleGrade(response);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			lv = (ListView) findViewById(R.id.list_view);
			lv.setAdapter(new ListAdapter(IvleGradeActivity.this,
					R.id.grade_book, grade));
	 }
	 
	 

		private class ListAdapter extends ArrayAdapter<IvleGradeBook> { // --
			// CloneChangeRequired
			private ArrayList<IvleGradeBook> mList; // --CloneChangeRequired
			private Context mContext;
			

			public ListAdapter(Context context, int textViewResourceId,
					ArrayList<IvleGradeBook> list) { // --CloneChangeRequired
				super(context, textViewResourceId, list);
				this.mList = list;
				this.mContext = context;
			}

			public View getView(int position, View convertView, ViewGroup parent) {
				View view = convertView;
				try {
					if (view == null) {
						LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
						view = vi.inflate(R.layout.ivlegradebookitem, null);
					}

					final IvleGradeBook listItem = mList.get(position);

					if (listItem != null) {
						// setting list_item views
						TextView tv14 = (TextView) view.findViewById(R.id.category);
						tv14.setText(listItem.getCategory()+":");

						TextView tv7 = (TextView) view.findViewById(R.id.item_name);
						tv7.setText(listItem.getItemName());
						
						TextView tv9 = (TextView) view.findViewById(R.id.marks_obtained);
						tv9.setText(listItem.getMarksObtained());

						TextView tv10 = (TextView) view.findViewById(R.id.max_marks);
						tv10.setText(listItem.getMaxMarks());
						
					}
				} catch (Exception e) {
					Log.i(IvleGradeActivity.ListAdapter.class.toString(), e.getMessage());
				}
				return view;
			}

		}
	 
	 
	 
	 
	 
	 public String getResponseText(String stringUrl) throws IOException
		{
			StringBuilder response  = new StringBuilder();

			URL url = new URL(stringUrl);
			HttpURLConnection httpconn = (HttpURLConnection)url.openConnection();
			if (httpconn.getResponseCode() == HttpURLConnection.HTTP_OK)
			{
				BufferedReader input = new BufferedReader(new InputStreamReader(httpconn.getInputStream()),8192);
				String strLine = null;
				while ((strLine = input.readLine()) != null)
				{
					response.append(strLine);
				}
				input.close();
			}
			return response.toString();
		}

}
